package com.menu.quartz.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.menu.common.utils.http.DwtHttpUtils;
import com.menu.system.service.IMenuProductsService;
import com.menu.system.service.IMenuProtypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.menu.common.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    private static final Logger log = LoggerFactory.getLogger(RyTask.class);
    private  String url;
    @Autowired
    private IMenuProductsService menuProductsService;
    @Autowired
    private IMenuProtypeService menuProtypeService;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }
    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


   /*德沃康同步产品管理*/
    public void dwkSynchronization()
    {
        JSONObject paramMap = new JSONObject();
        paramMap.put("userId", "admin");
        paramMap.put("password", "123456");
        String login = DwtHttpUtils.RestTemplateTestPost("api/index/login", paramMap, null);

        log.info("德沃康同步产品管理登录"+paramMap.toJSONString());



        String token = JSONObject.parseObject(login).getJSONObject("data").getString("token");

        log.info("德沃康同步产品管理登录token"+token);

        String types = DwtHttpUtils.RestTemplateTestGet("api/rz/get/type/list", new HashMap<String, Object>(), token);
        JSONArray data1 = JSONObject.parseObject(types).getJSONArray("data");
        List<String> data = data1.stream().map(t -> ((JSONObject) t).getString("typeName")).collect(Collectors.toList());
        List<String> collect = menuProtypeService.selectMenuProtypeids(data).stream().map(t -> t.getProductName()).collect(Collectors.toList());
        log.info("德沃康同步产品获取得类别"+collect);

        List<Object> typeName = data1.stream().filter(item -> collect.contains(((JSONObject) item).getString("typeName"))).collect(Collectors.toList());
        log.info("德沃康同步产品获取同类"+typeName);
        for (Object o : typeName) {

            JSONObject paramMap1 = new JSONObject();
            paramMap1.put("typeCode", JSONObject.from(o).get("id").toString());
            paramMap1.put("page", "1");


            String login1 = DwtHttpUtils.RestTemplateTestPost("api/rz/get/authen/page/list", paramMap1, token);
            paramMap1.put("limit", JSONObject.parseObject(login1).getJSONObject("data").getJSONObject("tableValue").get("total"));
            log.info("德沃康同步产品获取得类列表数"+JSONObject.parseObject(login1).getJSONObject("data").getJSONObject("tableValue").get("total"));
            String login2 = DwtHttpUtils.RestTemplateTestPost("api/rz/get/authen/page/list", paramMap1, token);

            JSONArray jsonArray = JSONObject.parse(login2).getJSONObject("data").getJSONObject("tableValue").getJSONArray("list");
            List<String> proModel = jsonArray.stream().map(t -> ((JSONObject) t).getString("proModel")).collect(Collectors.toList());

            List<String> colle = proModel.stream().distinct().collect(Collectors.toList());
            log.info("德沃康同步产品获取产品的类型"+colle);

        }


    }

}
